/*
 * Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.greenrobot.greendao.database;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

import ohos.app.Context;

class SqlCipherEncryptedHelper extends SQLiteOpenHelper implements DatabaseOpenHelper.EncryptedHelper {
    private final DatabaseOpenHelper delegate;

    /**
     * Constructor for encrypted helper
     * @param delegate database open helper
     * @param context context
     * @param name database name
     * @param version database version
     * @param loadLibs libs to load or not
     */
    public SqlCipherEncryptedHelper(DatabaseOpenHelper delegate, Context context, String name,
                                    int version, boolean loadLibs) {
        super(context, name, null, version);
        this.delegate = delegate;
        if (loadLibs) {
            SQLiteDatabase.loadLibs(context);
        }
    }

    private Database wrap(SQLiteDatabase sqLiteDatabase) {
        return new EncryptedDatabase(sqLiteDatabase);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        delegate.onCreate(wrap(db));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        delegate.onUpgrade(wrap(db), oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        delegate.onOpen(wrap(db));
    }

    @Override
    public Database getEncryptedReadableDb(String password) {
        return wrap(getReadableDatabase(password));
    }

    @Override
    public Database getEncryptedReadableDb(char[] password) {
        return wrap(getReadableDatabase(password));
    }

    @Override
    public Database getEncryptedWritableDb(String password) {
        return wrap(getWritableDatabase(password));
    }

    @Override
    public Database getEncryptedWritableDb(char[] password) {
        return wrap(getWritableDatabase(password));
    }

}
